package handler

import (
	"context"
	"fmt"
	"github.com/spf13/viper"
	"github.com/tedcy/fdfs_client"
	"house/dao/mysql"
	"house/proto" // 是目录结构层次，不包含包名
	"house/utils"
	"strconv"
)

type House struct{}

// Return a new handler
func New() *House {
	return &House{}
}

// GetUserHouses 获取用户房屋信息
func (e *House) GetUserHouses(ctx context.Context, req *house.GetReq) (*house.GetResp, error) {
	houses, err := mysql.GetUserHouse(req.UserName)
	if err != nil {
		return &house.GetResp{
			Errno:  utils.RECODE_DBERR,
			Errmsg: utils.RecodeText(utils.RECODE_DBERR),
			Data:   nil,
		}, nil
	}
	return &house.GetResp{
		Errno:  utils.RECODE_OK,
		Errmsg: utils.RecodeText(utils.RECODE_OK),
		Data: &house.GetData{
			Houses: houses,
		},
	}, nil
}

// PubHouse GetUserHouses 上传房屋信息
func (e *House) PubHouse(ctx context.Context, req *house.Request) (*house.Response, error) {
	housesID, err := mysql.PubHouse(req)
	if err != nil {
		fmt.Println("数据库存储房屋信息失败")
		return &house.Response{
			Errno:  utils.RECODE_DBERR,
			Errmsg: utils.RecodeText(utils.RECODE_DBERR),
			Data:   nil,
		}, nil
	}
	return &house.Response{
		Errno:  utils.RECODE_OK,
		Errmsg: utils.RecodeText(utils.RECODE_OK),
		Data: &house.HouseData{
			HouseId: strconv.Itoa(housesID),
		},
	}, nil
}

func (e *House) PostHousesImage(ctx context.Context, in *house.ImgReq) (*house.ImgResp, error) {
	// 上传头像到fastdfs中
	cli, _ := fdfs_client.NewClientWithConfig("/home/uos/go/src/myweb/web/conf/fdfs.conf")
	//err := ctx.SaveUploadedFile(file, "./test/"+file.Filename)
	//if err != nil {
	//	fmt.Println("savauploadfile failed: ", err)
	//}

	// 按字节流上传图片内容
	remoteID, err := cli.UploadByBuffer(in.ImgData, in.FileExt[1:])
	if err != nil {
		return &house.ImgResp{
				Errno:  utils.RECODE_DATAERR,
				Errmsg: utils.RecodeText(utils.RECODE_DATAERR),
				Data:   nil,
			},
			nil
	}
	fmt.Println("remoteId:= ", remoteID)

	// 根据房屋ID，更新房屋图像
	err = mysql.UpdateHouseImage(in.HouseId, remoteID)
	if err != nil {
		return &house.ImgResp{
				Errno:  utils.RECODE_DBERR,
				Errmsg: utils.RecodeText(utils.RECODE_DBERR),
				Data:   nil,
			},
			nil
	}

	fmt.Println("house微服务（上传图片）被调用成功。")

	return &house.ImgResp{
			Errno:  utils.RECODE_OK,
			Errmsg: utils.RecodeText(utils.RECODE_OK),
			Data: &house.ImgData{
				Url: "http://" + viper.GetString("server.localAddress") + "/" + remoteID,
			},
		},
		nil

	//resp := make(map[string]interface{})
	//resp["index_image_url"] = "http://" + viper.GetString("server.localAddress") + "/" + remoteID
}

// GetHouseDetail 获取房屋详情
func (e *House) GetHouseDetail(ctx context.Context, req *house.DetailReq) (*house.DetailResp, error) {
	//根据用户名获取所有的房屋数据
	respData, err := mysql.GetHouseDetail(req.HouseId, req.UserName)
	if err != nil {
		fmt.Println("获取房屋详情失败")
		return &house.DetailResp{
			Errno:  utils.RECODE_DBERR,
			Errmsg: utils.RecodeText(utils.RECODE_DBERR),
			Data:   nil,
		}, nil
	}
	return &house.DetailResp{
		Errno:  utils.RECODE_OK,
		Errmsg: utils.RecodeText(utils.RECODE_OK),
		Data:   &respData,
	}, nil
}

func (e *House) GetIndexHouse(ctx context.Context, in *house.IndexReq) (*house.GetResp, error) {
	houses, err := mysql.GetIndexHouse()
	if err != nil {
		return &house.GetResp{
			Errno:  utils.RECODE_DBERR,
			Errmsg: utils.RecodeText(utils.RECODE_DBERR),
			Data:   nil,
		}, nil
	}
	return &house.GetResp{
		Errno:  utils.RECODE_OK,
		Errmsg: utils.RecodeText(utils.RECODE_OK),
		Data: &house.GetData{
			Houses: houses,
		},
	}, nil
}

// SearchHouse 查询房屋
func (e *House) SearchHouse(ctx context.Context, req *house.SearchReq) (*house.GetResp, error) {
	houses, err := mysql.SearchHouse(req.Aid, req.Sd, req.Ed, req.Sk)
	if err != nil {
		return &house.GetResp{
			Errno:  utils.RECODE_DBERR,
			Errmsg: utils.RecodeText(utils.RECODE_DBERR),
			Data:   nil,
		}, nil
	}
	return &house.GetResp{
		Errno:  utils.RECODE_OK,
		Errmsg: utils.RecodeText(utils.RECODE_OK),
		Data: &house.GetData{
			Houses: houses,
		},
	}, nil
}
